package gui;

import guiCode.parentViewCode;

import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextPane;
import javax.swing.JEditorPane;

import com.sun.rowset.CachedRowSetImpl;

import basic.ClientConsole;

import java.awt.SystemColor;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;

public class parentViewScreen  extends JFrame {
	
	private JTable table;
	private JComboBox studentComboBox;
	private JComboBox lowerComboBox;
	private JLabel lblStudentCoursesAndGrades;
	private JTextPane textPane_1 ;
	private JLabel lblStudentId;
	private JTextPane textPane;
	private JLabel lblClass;
	private JLabel lblPhoneNumber;
	private JScrollPane scrollPane;
	private JLabel lblStudentName;
	private JLabel lblInfo;
	private JLabel lblHeader;
	private parentViewCode pvc=new parentViewCode();
	private JTextPane textPanePhone;
	
	public parentViewScreen(ClientConsole conn, Integer whoAmI) {
		setTitle("Parent View Screen");
		
		pvc.setConnection(conn);
		pvc.setID(whoAmI);
		
	    SpringLayout springLayout = new SpringLayout();
		this.getContentPane().setLayout(springLayout);
		
		
		lblHeader = new JLabel(pvc.getParentName());
		
		springLayout.putConstraint(SpringLayout.NORTH, lblHeader, 10, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblHeader, 178, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblHeader, 46, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblHeader, -195, SpringLayout.EAST, getContentPane());
		lblHeader.setFont(new Font("Tahoma", Font.PLAIN, 30));
		getContentPane().add(lblHeader);
		
		lblInfo = new JLabel("You can watch student  file about:");
		springLayout.putConstraint(SpringLayout.NORTH, lblInfo, 29, SpringLayout.SOUTH, lblHeader);
		springLayout.putConstraint(SpringLayout.WEST, lblInfo, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblInfo);
		
		studentComboBox = new JComboBox(pvc.getParentStudents());
		springLayout.putConstraint(SpringLayout.NORTH, studentComboBox, 37, SpringLayout.SOUTH, lblHeader);
		springLayout.putConstraint(SpringLayout.WEST, studentComboBox, 239, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, studentComboBox, -144, SpringLayout.EAST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblInfo, -6, SpringLayout.WEST, studentComboBox);
		getContentPane().add(studentComboBox);
		
		lblStudentName = new JLabel("Student Name's personal Details:");
		springLayout.putConstraint(SpringLayout.NORTH, lblStudentName, 133, SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblInfo, -22, SpringLayout.NORTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.WEST, lblStudentName, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblStudentName);
		
		scrollPane = new JScrollPane();
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 32, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -54, SpringLayout.SOUTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, -37, SpringLayout.EAST, getContentPane());
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] {},new String[] {}));
		scrollPane.setViewportView(table);
		
		lblPhoneNumber = new JLabel("Phone number: ");
		springLayout.putConstraint(SpringLayout.NORTH, lblPhoneNumber, 70, SpringLayout.SOUTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.WEST, lblPhoneNumber, 30, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblPhoneNumber);
		
		lblClass = new JLabel("Current Class:");
		springLayout.putConstraint(SpringLayout.NORTH, lblClass, 27, SpringLayout.SOUTH, lblStudentName);
		springLayout.putConstraint(SpringLayout.WEST, lblClass, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblClass);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setBackground(SystemColor.control);
		springLayout.putConstraint(SpringLayout.NORTH, textPane, 0, SpringLayout.NORTH, lblClass);
		springLayout.putConstraint(SpringLayout.WEST, textPane, 51, SpringLayout.EAST, lblClass);
		springLayout.putConstraint(SpringLayout.EAST, textPane, 184, SpringLayout.EAST, lblClass);
		getContentPane().add(textPane);
		
		lblStudentId = new JLabel("Student ID");
		springLayout.putConstraint(SpringLayout.NORTH, lblStudentId, 20, SpringLayout.SOUTH, lblPhoneNumber);
		springLayout.putConstraint(SpringLayout.WEST, lblStudentId, 32, SpringLayout.WEST, getContentPane());
		getContentPane().add(lblStudentId);
		
		textPane_1 = new JTextPane();
		textPane_1.setEditable(false);
		springLayout.putConstraint(SpringLayout.NORTH, textPane_1, 57, SpringLayout.SOUTH, textPane);
		springLayout.putConstraint(SpringLayout.WEST, textPane_1, 152, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, lblStudentId, -50, SpringLayout.WEST, textPane_1);
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 71, SpringLayout.SOUTH, textPane_1);
		textPane_1.setBackground(SystemColor.control);
		springLayout.putConstraint(SpringLayout.EAST, textPane_1, 0, SpringLayout.EAST, textPane);
		getContentPane().add(textPane_1);
		
		lblStudentCoursesAndGrades = new JLabel("Student Courses And grades:");
		springLayout.putConstraint(SpringLayout.WEST, lblStudentCoursesAndGrades, 32, SpringLayout.WEST, getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, lblStudentCoursesAndGrades, -18, SpringLayout.NORTH, scrollPane);
		getContentPane().add(lblStudentCoursesAndGrades);
		
		lowerComboBox = new JComboBox();
		lowerComboBox.setModel(new DefaultComboBoxModel(new String[] {"Current Semester Assigment", "Previous semesters"}));
		springLayout.putConstraint(SpringLayout.NORTH, lowerComboBox, -3, SpringLayout.NORTH, lblStudentCoursesAndGrades);
		springLayout.putConstraint(SpringLayout.WEST, lowerComboBox, 80, SpringLayout.EAST, lblStudentCoursesAndGrades);
		springLayout.putConstraint(SpringLayout.EAST, lowerComboBox, -72, SpringLayout.EAST, getContentPane());
		getContentPane().add(lowerComboBox);
		
		textPanePhone = new JTextPane();
		textPanePhone.setEditable(false);
		textPanePhone.setBackground(SystemColor.control);
		springLayout.putConstraint(SpringLayout.NORTH, textPanePhone, 0, SpringLayout.NORTH, lblPhoneNumber);
		springLayout.putConstraint(SpringLayout.WEST, textPanePhone, 0, SpringLayout.WEST, textPane);
		springLayout.putConstraint(SpringLayout.EAST, textPanePhone, 188, SpringLayout.EAST, lblPhoneNumber);
		getContentPane().add(textPanePhone);
		
		setParentListeners();
		
		this.setSize(600,700);
		this.setLocation(100,10);
		this.setVisible(true);

	}
	private void setParentListeners()
	{
		
		
		studentComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				 String s;
				 JComboBox cb = (JComboBox)e.getSource();
				 s = (String)cb.getSelectedItem();
				 s= s.substring(0, s.indexOf(" ")); //take StudentID from selected string 
				//System.out.print(s+"\n");
				if(pvc.isStudentBlocked(s))
				 {
					System.out.print("Student Blocked \n" );
				 }
				else
					System.out.print("Student Not Blocked \n" );
			}
		});
	}
	
}
